package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.anno.Log;
import org.example.pojo.Clazz;
import org.example.pojo.ClazzQueryParam;
import org.example.pojo.PageResult;
import org.example.pojo.Result;
import org.example.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController{
    @Autowired
    private  ClazzService clazzService;
    @GetMapping
    public Result list(ClazzQueryParam clazzQueryParam){
        log.info("查询所有班级");
        PageResult<Clazz> pageResult = clazzService.list(clazzQueryParam);
        return Result.success(pageResult);
    }

    //在新增学员的时候，要展示出所有的班级信息
    @GetMapping("/list")
    public Result clazzList(){
        log.info("查询所有班级");
        List<Clazz> clazz = clazzService.listAll();
        return Result.success(clazz);
    }

    // 新增班级
    @Log
    @PostMapping
    public Result add(@RequestBody Clazz clazz){
        log.info("新增班级:{}",clazz);
        clazzService.addClazz(clazz);
        return Result.success();
    }

    //根据id查询班级信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询班级信息：{}", id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }
    // 修改班级
    @Log
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("修改班级信息：{}", clazz);
        clazzService.updateClazz(clazz);
        return Result.success();
    }

    // 根据ID删除班级
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除班级：{}", id);
        clazzService.deleteById(id);
        return Result.success();
    }

}
